package com.hnhs.enums;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Optional;

//枚举类提供的状态信息
public enum ProductStatus {
    ONLINE(1,"上线"),
    OFFLINE(5,"下线"),
    DISABLE(10,"禁售");

    @Getter
    @Setter
    private Integer code;
    @Getter
    @Setter
    private String msg;

    ProductStatus(Integer code,String msg){
        this.code=code;
        this.msg=msg;
    }

    public static ProductStatus findByCode(Integer code){
        if(code == null){
            return null;
        }
        //传递枚举信息 你前台传过来的枚举信息 需要后台导出
        Optional<ProductStatus> first = Arrays.stream(ProductStatus.values())
                .filter(item -> item.getCode().equals(code)).findFirst();
        //如果first 有数据就直接返回
        if(first.isPresent()){
            return first.get();
        }
        return  first.orElse(null);
    }
}
